package com.devin.controller;

import com.devin.pojo.Orders;
import com.devin.pojo.OrdersAndTraveller;
import com.devin.pojo.Product;
import com.devin.service.LogService;
import com.devin.service.OrdersService;
import com.devin.service.ProductService;
import com.devin.utils.FieldUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public class OrdersController {
    @Autowired
    @Qualifier(value = "ordersService")
    private OrdersService ordersService;

    @Autowired
    private ProductService productService;

    //    @RequestMapping(value = "/getOrders",method = RequestMethod.GET)
//    public ModelAndView getOrders(){
//        List<Orders> ordersList = ordersService.findOrders();
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("ordersList",ordersList);
//        modelAndView.setViewName("orders-list");
//        return  modelAndView;
//    }
    /* --------- 增添分页功能 ---------*/
    @RequestMapping("/getOrders")
    @Secured("ROLE_USER")

    public ModelAndView getOrders(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "3") Integer size) {
        List<Orders> ordersList = ordersService.findOrders(page, size);
        PageInfo<Orders> orderPageInfo = new PageInfo<Orders>(ordersList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo", orderPageInfo);
        modelAndView.setViewName("orders-pages-list");
        return modelAndView;
    }

    @RequestMapping("/newOrder")
    public ModelAndView newOrder(){
        List<Product> allProducts = productService.getAllProducts();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("productsAdd",allProducts);
        modelAndView.setViewName("order-add");
        return modelAndView;
    }

    @RequestMapping("/addOrder")
    @ResponseBody
    public void addOrder(@RequestBody OrdersAndTraveller ordersAndTraveller) {
        ordersService.addOrder(ordersAndTraveller);
//      在ajax success函数里面使用location重定向到orders/getOrders
    }

    @RequestMapping("/getOrdersInfo")
    public ModelAndView getOrdersInfo(Integer orderId) {
        Orders orders = ordersService.getOrdersInfoById(orderId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("orders", orders);
        int size = orders.getTravellerList().size();
        System.out.println(size);

        //判断乘客人数，进入不同的页面
        if (size != 0 && orders.getTravellerList()!=null ) {
            if (size == 1) {
                modelAndView.setViewName("orders-detail");
            }else if (size == 2 /*& FieldUtils.allFieldIsNotNull(orders.getTravellerList().get(1))*/) {
                modelAndView.setViewName("orders-detail-two");
            }/*else{
                modelAndView.setViewName("orders-detail");
            }*/
        } else{
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }

    @RequestMapping("/delOrder")
    public String delOrder(@RequestParam("orderId") Integer orderId,@RequestParam("memberId")Integer memberId){
        ordersService.delOrder(orderId,memberId);
        return "redirect:/orders/getOrders";
    }

}
